﻿// AutoCam.cpp: 实现文件
//

#include "pch.h"
#include "LiWi.h"
#include "afxdialogex.h"
#include "AutoCam.h"
#include "liwishell.h"
#include "Data.h"


// AutoCam 对话框

IMPLEMENT_DYNAMIC(AutoCam, CDialogEx)

AutoCam::AutoCam(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_AutoCam, pParent)
{

}

AutoCam::~AutoCam()
{
}

void AutoCam::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(AutoCam, CDialogEx)
	ON_BN_CLICKED(IDC_StartAuto, &AutoCam::OnBnClickedStartauto)
END_MESSAGE_MAP()


// AutoCam 消息处理程序


BOOL AutoCam::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
#pragma region 设置位置规划参数
	GetDlgItem(IDC_Acc1_Point)->SetWindowTextW(_T("50"));
	GetDlgItem(IDC_Acc2_Point)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_Acc3_Point)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_Acc4_Point)->SetWindowTextW(_T("0"));
	GetDlgItem(IDC_Vel1_Point)->SetWindowTextW(_T("200"));
	GetDlgItem(IDC_Vel2_Point)->SetWindowTextW(_T("200"));
	GetDlgItem(IDC_Vel3_Point)->SetWindowTextW(_T("0"));
	GetDlgItem(IDC_Vel4_Point)->SetWindowTextW(_T("0"));
	GetDlgItem(IDC_Dec1_Point)->SetWindowTextW(_T("50"));
	GetDlgItem(IDC_Dec2_Point)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_Dec3_Point)->SetWindowTextW(_T("0"));
	GetDlgItem(IDC_Dec4_Point)->SetWindowTextW(_T("100"));
#pragma endregion

#pragma region 设置速度规划参数
	GetDlgItem(IDC_Acc1_Speed)->SetWindowTextW(_T("50"));
	GetDlgItem(IDC_Acc2_Speed)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_Acc3_Speed)->SetWindowTextW(_T("1.00"));
	GetDlgItem(IDC_Acc4_Speed)->SetWindowTextW(_T("1.00"));
	GetDlgItem(IDC_Vel1_Speed)->SetWindowTextW(_T("200"));
	GetDlgItem(IDC_Vel2_Speed)->SetWindowTextW(_T("200"));
	GetDlgItem(IDC_Vel3_Speed)->SetWindowTextW(_T("1.00"));
	GetDlgItem(IDC_Vel4_Speed)->SetWindowTextW(_T("1.00"));
	GetDlgItem(IDC_Dec1_Speed)->SetWindowTextW(_T("50"));
	GetDlgItem(IDC_Dec2_Speed)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_Dec3_Speed)->SetWindowTextW(_T("1.00"));
	GetDlgItem(IDC_Dec4_Speed)->SetWindowTextW(_T("1.00"));
#pragma endregion
	for (int i = 0; i < 4; i++)
	{
		CString strAxis;
		strAxis.Format(_T("%d"), i);
		((CComboBox*)GetDlgItem(IDC_FirstAxis))->AddString(strAxis);
		((CComboBox*)GetDlgItem(IDC_SecendAxis))->AddString(strAxis);
	}
	((CComboBox*)GetDlgItem(IDC_FirstAxis))->SetCurSel(0);
	((CComboBox*)GetDlgItem(IDC_SecendAxis))->SetCurSel(1);

	GetDlgItem(IDC_MoveLine2)->SetWindowTextW(_T("200"));

	((CButton*)GetDlgItem(IDC_PointMode))->SetCheck(true);




	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}



void AutoCam::OnBnClickedStartauto()
{
	// TODO: 在此添加控件通知处理程序代码
	CString axisList[2],moveLine;
	int enable;
	GetDlgItem(IDC_FirstAxis)->GetWindowTextW(axisList[0]);
	GetDlgItem(IDC_SecendAxis)->GetWindowTextW(axisList[1]);
	GetDlgItem(IDC_MoveLine2)->GetWindowTextW(moveLine);
	if (axisList[0]==axisList[1])
	{
		MessageBox(_T("主轴与从轴不能同一轴"));
		return;
	}
	for (int i = 0; i < 2; i++)
	{
		
		PMC_GetAxisEnable(Data::handle, _ttoi(axisList[i]), &enable);
		if (enable==0)
		{
			MessageBox(_T("请先对轴使能！！！"));
			return;
		}
		PMC_SetAtype(Data::handle, _ttoi(axisList[i]), 1);
		PMC_SetSpeed(Data::handle, _ttoi(axisList[i]), 100);
		PMC_SetUnits(Data::handle, _ttoi(axisList[i]), 100);
		PMC_SetAccel(Data::handle, _ttoi(axisList[i]), 1000);
		PMC_SetDecel(Data::handle, _ttoi(axisList[i]), 1000);
	}
	CString value1, value2, value3, value4;
	if (((CButton*)GetDlgItem(IDC_PointMode))->GetCheck())	//位置模式
	{
		
		GetDlgItem(IDC_Acc1_Point)->GetWindowTextW(value1);
		GetDlgItem(IDC_Acc2_Point)->GetWindowTextW(value2);
		GetDlgItem(IDC_Acc3_Point)->GetWindowTextW(value3);
		GetDlgItem(IDC_Acc4_Point)->GetWindowTextW(value4);
		PMC_Movelink(Data::handle, _ttoi(axisList[1]), _ttof(value1), _ttof(value2), _ttof(value3), _ttof(value4), _ttoi(axisList[0]), 0, 0);
		//PMC_MoveOp(Data::handle, _ttoi(axisList[1]), 10, 1);
		GetDlgItem(IDC_Vel1_Point)->GetWindowTextW(value1);
		GetDlgItem(IDC_Vel2_Point)->GetWindowTextW(value2);
		GetDlgItem(IDC_Vel3_Point)->GetWindowTextW(value3);
		GetDlgItem(IDC_Vel4_Point)->GetWindowTextW(value4);
		PMC_Movelink(Data::handle, _ttoi(axisList[1]), _ttof(value1), _ttof(value2), _ttof(value3), _ttof(value4), _ttoi(axisList[0]), 0, 0);
		//PMC_MoveOp(Data::handle, _ttoi(axisList[1]), 10, 0);
		GetDlgItem(IDC_Dec1_Point)->GetWindowTextW(value1);
		GetDlgItem(IDC_Dec2_Point)->GetWindowTextW(value2);
		GetDlgItem(IDC_Dec3_Point)->GetWindowTextW(value3);
		GetDlgItem(IDC_Dec4_Point)->GetWindowTextW(value4);
		PMC_Movelink(Data::handle, _ttoi(axisList[1]), _ttof(value1), _ttof(value2), _ttof(value3), _ttof(value4), _ttoi(axisList[0]), 0, 0);
	}
	else
	{
		try
		{
			GetDlgItem(IDC_Acc1_Speed)->GetWindowTextW(value1);
			GetDlgItem(IDC_Acc2_Speed)->GetWindowTextW(value2);
			GetDlgItem(IDC_Acc3_Speed)->GetWindowTextW(value3);
			GetDlgItem(IDC_Acc4_Speed)->GetWindowTextW(value4);
			PMC_Moveslink(Data::handle, _ttoi(axisList[1]), _ttof(value1), _ttof(value2), _ttof(value3), _ttof(value4), _ttoi(axisList[0]), 0, 0);
			//PMC_MoveOp(Data::handle, _ttoi(axisList[1]), 10, 1);
			GetDlgItem(IDC_Vel1_Speed)->GetWindowTextW(value1);
			GetDlgItem(IDC_Vel2_Speed)->GetWindowTextW(value2);
			GetDlgItem(IDC_Vel3_Speed)->GetWindowTextW(value3);
			GetDlgItem(IDC_Vel4_Speed)->GetWindowTextW(value4);
			PMC_Moveslink(Data::handle, _ttoi(axisList[1]), _ttof(value1), _ttof(value2), _ttof(value3), _ttof(value4), _ttoi(axisList[0]), 0, 0);
			//PMC_MoveOp(Data::handle, _ttoi(axisList[1]), 10, 0);
			GetDlgItem(IDC_Dec1_Speed)->GetWindowTextW(value1);
			GetDlgItem(IDC_Dec2_Speed)->GetWindowTextW(value2);
			GetDlgItem(IDC_Dec3_Speed)->GetWindowTextW(value3);
			GetDlgItem(IDC_Dec4_Speed)->GetWindowTextW(value4);
			PMC_Moveslink(Data::handle, _ttoi(axisList[1]), _ttof(value1), _ttof(value2), _ttof(value3), _ttof(value4), _ttoi(axisList[0]), 0, 0);
		}
		catch (const std::exception&)
		{
			MessageBox(_T("比例请输入小数"));
			return;
		}
	}
	PMC_Single_Move(Data::handle, _ttoi(axisList[0]), _ttof(moveLine));
}
